home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / main / macros.c < prev    next >
Text File  |  1994-04-14  |  7KB  |  339 lines

  1. /*
  2. *    macros.c
  3. *     by Gaige B. Paulsen
  4. *****************************************************************
  5. *    NCSA Telnet for the Macintosh                                *
  6. *                                                                *
  7. *    National Center for Supercomputing Applications                *
  8. *    Software Development Group                                    *
  9. *    152 Computing Applications Building                            *
  10. *    605 E. Springfield Ave.                                        *
  11. *    Champaign, IL  61820                                        *
  12. *                                                                *
  13. *    Copyright (c) 1986-1992,                                    *
  14. *    Board of Trustees of the University of Illinois                *
  15. *****************************************************************
  16. *    7/92    Moved here from event.c and maclook.c by JMB
  17. */
  18.  
  19. #ifdef MPW
  20. #pragma segment Macros
  21. #endif
  22.  
  23. /* Macro Defines */
  24. #define MACRO_IP        0xff    /* Send IP number here */
  25. #define MACRO_LINES        0xfe    /* Send # of lines here */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30.  
  31. #include "TelnetHeader.h"
  32. #include "network.proto.h"                /* For netwrite proto */
  33. #include "wind.h"                /* For WindRec definition */
  34. #include "DlogUtils.proto.h"
  35. #include "translate.proto.h"
  36. #include "parse.proto.h"
  37. #include "ftpbin.proto.h"
  38. #include "event.proto.h"
  39.  
  40. #include "vsdata.h"
  41. #include "vsinterf.proto.h"
  42.  
  43. #include "macros.proto.h"
  44.  
  45. extern WindRec *screens;
  46. extern short scrn;
  47. extern Cursor *theCursors[];
  48.  
  49. unsigned char *macro[10];    /* the wonderful macro package .... */
  50.  
  51. void    MACROSunload(void) {}
  52.  
  53. void setmacro( short n, unsigned char *s)            /* Set macro number <n> to the value of s */
  54. {
  55.     unsigned char *p;
  56.     short num=0, pos=0, escape=0;
  57.  
  58.     if (n<0  || n>9)
  59.         return;
  60.  
  61.     p=macro[n];
  62.  
  63.     while ( *s) {
  64.         if (escape) {
  65.             escape=0;
  66.             switch (*s) {
  67.                 case 'i':
  68.                     if ( pos >0) {
  69.                         *p++=num;
  70.                         *p++=*s;
  71.                         pos=0;
  72.                         }
  73.                     *p++=MACRO_IP;
  74.                     break;
  75.                 case '#':
  76.                     if ( pos >0) {
  77.                         *p++=num;
  78.                         *p++=*s;
  79.                         pos=0;
  80.                         }
  81.                     *p++=MACRO_LINES;
  82.                     break;
  83.                 case 'n':
  84.                     if ( pos >0) {
  85.                         *p++=num;
  86.                         *p++=*s;
  87.                         pos=0;
  88.                         }
  89.                     *p++='\012';
  90.                     break;
  91.                 case 'r':
  92.                     if ( pos >0) {
  93.                         *p++=num;
  94.                         *p++=*s;
  95.                         pos=0;
  96.                         }
  97.                     *p++='\015';
  98.                     break;
  99.                 case 't':
  100.                     if ( pos >0) {
  101.                         *p++=num;
  102.                         *p++=*s;
  103.                         pos=0;
  104.                         }
  105.                     *p++='\t';
  106.                     break;
  107.                 case '"':
  108.                     if ( pos >0) {
  109.                         *p++=num;
  110.                         *p++=*s;
  111.                         pos=0;
  112.                         }
  113.                     *p++='\"';
  114.                     break;
  115.  
  116.                         
  117.                 case '\\':
  118.                     if ( pos >0) {
  119.                         *p++=num;
  120.                         escape=1;
  121.                         pos=0;
  122.                         num=0;
  123.                         }
  124.                     else
  125.                         *p++='\\';
  126.                     break;
  127.                 default:
  128.                     if (*s <='9' && *s >='0' && pos <3) {
  129.                         num= num*8+( *s -'0');
  130.                         pos++;
  131.                         escape=1;
  132.                         }
  133.                     else {
  134.                         if (pos ==0 && num==0) {
  135.                             *p++='\\';
  136.                             *p++=*s;
  137.                             }
  138.                         else {
  139.                             *p++=num;
  140.                             pos= 0;
  141.                             s--;            /* back up the buffer. */
  142.                             }
  143.                         }
  144.                     break;
  145.                 }
  146.             }
  147.         else {
  148.             if (*s=='\\') {
  149.                 num=0;
  150.                 pos=0;
  151.                 escape=1;
  152.                 }
  153.             else
  154.                 *p++=*s;
  155.             }
  156.         s++;
  157.         }
  158.  
  159.     if (pos >0) *p++=num;
  160.     *p=0;
  161. } /* setmacro */
  162.  
  163. short sendmacro( short n)                /* send macro number n */
  164. {
  165.     unsigned char temp[300], *mp, *tp;
  166.     unsigned char myipnum[4];
  167.  
  168.     if (n<0 || n>9) return(-1);
  169.  
  170.     tp = temp;
  171.     mp = macro[n];
  172.     netgetip(myipnum);
  173.     while ( *mp) {
  174.         if ( *mp==MACRO_IP) {
  175.             sprintf((char *) tp,"%d.%d.%d.%d", myipnum[0], myipnum[1], myipnum[2], myipnum[3]);        /* BYU LSC */
  176.             tp+=strlen((char *) tp);    /* BYU LSC */
  177.             mp++;
  178.             }
  179.         else if ( *mp==MACRO_LINES) {
  180.             sprintf((char *) tp,"%d", VSgetlines( screens[scrn].vs));    /* BYU LSC */
  181.             tp+=strlen((char *) tp);    /* BYU LSC */
  182.             mp++;
  183.             }
  184.         else if (screens[scrn].ftpstate) {                                /* BYU 2.4.16 */
  185.             if (*mp == CR) {                                            /* BYU 2.4.16 */
  186.                 parse( &screens[scrn],(unsigned char *) "\015\012",2);    /* BYU 2.4.16 */
  187.                 screens[scrn].kbbuf[ screens[scrn].kblen++ ] = 0;        /* BYU 2.4.16 */
  188.                 ftppi(screens[scrn].kbbuf);                                /* BYU 2.4.16 - ftp client */
  189.                 screens[scrn].kblen=0;                                    /* BYU 2.4.16 */
  190.                 mp++;                                                    /* BYU 2.4.16 */
  191.             } else {                                                    /* BYU 2.4.16 */
  192.                 screens[scrn].kbbuf[ screens[scrn].kblen++ ] = *mp;        /* BYU 2.4.16 */
  193.                 parse( &screens[ scrn], mp++, 1);                        /* BYU 2.4.16 */
  194.             }                                                            /* BYU 2.4.16 */
  195.         } else *tp++=*mp++;                                                /* BYU 2.4.16 */
  196.     }                                                                    /* BYU 2.4.16 */
  197.     *tp=0;                        /* Gotta have a nul! */
  198.     tp= temp;
  199.  
  200.     trbuf_nat_mac((unsigned char *)tp, strlen((char *)tp), screens[scrn].national);        /* LU */
  201.  
  202.     if (!screens[scrn].ftpstate) {        /* BYU 2.4.16 */
  203.  
  204.         netpush( screens[scrn].port);                                    /* BYU 2.4.16 */
  205.  
  206.         if (screens[scrn].lmode)     /* need to flush buffer */            /* BYU 2.4.16 */
  207.             {                                                            /* BYU 2.4.16 */
  208.             netwrite(screens[scrn].port,screens[scrn].kbbuf,screens[scrn].kblen);    /* BYU 2.4.16 */
  209.             screens[scrn].kblen=0;                                        /* BYU 2.4.16 */
  210.             }                                                            /* BYU 2.4.16 */
  211.     }                                                                    /* BYU 2.4.16 */
  212.  
  213.     if (!screens[scrn].ftpstate) {                                        /* BYU 2.4.16 */
  214.         netwrite(screens[scrn].port, tp, strlen((char *) tp) );            /* BYU LSC */
  215.     
  216.         if (screens[scrn].echo)
  217.             parse( &screens[scrn],tp, strlen((char *) tp) );            /* BYU LSC */
  218.     }                                                                    /* BYU 2.4.16 */
  219.  
  220.     return(0);
  221. }
  222.  
  223. short getmacro( short n, unsigned char *dest)
  224. {
  225.     unsigned char *s;
  226.  
  227.     if (n<0 || n>9)
  228.         return(-1);
  229.  
  230.     s = macro[n];
  231.     while (*s) {
  232.         switch( *s) {
  233.         case MACRO_IP :
  234.             *dest++='\\';
  235.             *dest++='i';
  236.             break;
  237.         case MACRO_LINES :
  238.             *dest++='\\';
  239.             *dest++='#';
  240.             break;
  241.         case '\\':
  242.             *dest++='\\';
  243.             *dest++='\\';
  244.             break;
  245.         case '\015':
  246.             *dest++='\\';
  247.             *dest++='r';
  248.             break;
  249.         case '\012':
  250.             *dest++='\\';
  251.             *dest++='n';
  252.             break;
  253.             
  254. #ifdef OLD_CRAP
  255.         case '\015':
  256.             s++;
  257.             if (*s == '\012' || *s=='\000') {
  258.                 *dest++='\\';
  259.                 *dest++='n';
  260.                 }
  261.             else {
  262.                 *dest++='\\';
  263.                 *dest++='0';
  264.                 *dest++='1';
  265.                 *dest++='5';
  266.                 s--;
  267.                 }
  268.             break;
  269. #endif
  270.         case '\t':
  271.             *dest++='\\';
  272.             *dest++='t';
  273.             break;
  274.         default: 
  275.             if ( isprint(*s)) 
  276.                 *dest++=*s;
  277.             else {
  278.                 *dest++='\\';
  279.                 *dest++= (*s / 64) +'0';
  280.                 *dest++= ((*s % 64) / 8)+'0';
  281.                 *dest++= (*s % 8) +'0';
  282.                 }
  283.             break;
  284.             }
  285.         s++;
  286.         }
  287.     *dest=0;
  288.     return( 0);
  289. }
  290.  
  291. void Macros( void)
  292. {
  293.     DialogPtr dtemp;
  294.     short dItem;
  295.     short i;
  296.     Rect dBox;
  297.     Str255 temp;
  298.     Handle MacString[10];
  299.  
  300.     SetCursor(theCursors[normcurs]);
  301.  
  302.     dtemp=GetNewMyDialog( MacroDLOG, NULL, kInFront, (void *)ThirdCenterDialog);
  303.  
  304.     for (i=0; i<10; i++) {
  305.         getmacro( i, (unsigned char *) &temp);        /* BYU LSC */
  306.         c2pstr((char *)temp);                                /* BYU LSC */
  307.         GetDItem( dtemp, i+13, &dItem, &MacString[i], &dBox);
  308.         SetIText( MacString[i], temp );
  309.         }
  310.  
  311.     dItem=0;                                /* initially no hits */
  312.     while((dItem>2) || (dItem==0)) {        /* While we are in the loop */
  313.         ModalDialog(DLOGwOK_CancelUPP,&dItem);
  314.         if (dItem >2 && dItem <13) {
  315.             i=dItem-3;
  316.             getmacro( i, (unsigned char *) &temp);            /* BYU LSC */
  317.             c2pstr((char *)temp);
  318.             GetDItem( dtemp, i+13, &dItem, &MacString[i], &dBox);
  319.             SetIText( MacString[i], temp );                /* BYU LSC - Revert the mother */
  320.             SelIText( dtemp, i+13, 0, 32767);                /* And select it... */
  321.             }
  322.         }
  323.         
  324.     updateCursor(1);
  325.     
  326.     if (dItem==DLOGCancel) {
  327.             DisposDialog( dtemp);
  328.             return;
  329.             }
  330.  
  331.     for (i=0; i<10; i++) {
  332.         GetIText( MacString[i], temp);
  333.         p2cstr(temp);
  334.         setmacro(i, (unsigned char *) &temp);
  335.         }
  336.  
  337.     DisposDialog( dtemp);
  338. }
  339.